﻿package net.wangit.view.unit.component;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.view.element.AutoLabelElement;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.component.CheckBoxFieldElement;
import net.wangit.view.element.component.LinkedSelectElement;
import net.wangit.view.element.component.RadioElement;
import net.wangit.view.element.component.SelectElement;
import net.wangit.view.enumeration.EnumerationFactory;
import net.wangit.view.unit.UnitWriter;
import net.wangit.view.unit.grid.Group;
import net.wangit.view.unit.util.QueryElementUtil;
import net.wangit.view.unit.util.ValidationProcessor;

public class QueryUnit extends FormUnit {

	private String orderElementId;

	public QueryUnit() {
	}

	protected void configure(Row row1) {
	}

	public void addElement2Group(ActionContext ac, Group group,
			ElementComponent ec) {
		if (ec.isAddLabel() && ec.getTdColSpan() > 0)
			group.addElement(new AutoLabelElement(ec.getName()));
		if (ec instanceof RadioElement)
			QueryElementUtil.addAll(ac, ec);
		else if ((ec instanceof SelectElement)
				&& !(ec instanceof LinkedSelectElement))
			QueryElementUtil.addBlank(ec);
		else if (ec instanceof CheckBoxFieldElement)
			QueryElementUtil.setCheckBoxFieldRadio(ac, ec);
		group.addElement(ec);
		int queryMode = ec.getQueryMode();
		if (queryMode == 4) {
			ElementComponent to = getScopeElement(ac, ec);
			ValidationProcessor.setInvalidField(ac, this, to);
			group.addElement(to);
		} else if (queryMode == 3)
			group.addElement(getOptionElement(ac, ec));
	}

	public boolean isRemember() {
		return true;
	}

	public UnitWriter getWriter() {
		return new QueryUnitWriter();
	}

	public boolean isOperable() {
		return false;
	}

	public boolean isAddable() {
		return false;
	}

	public boolean isEditable() {
		return false;
	}

	public String getShowMode() {
		return "e";
	}

	protected void initShowMode(ActionContext actioncontext) {
	}

	private ElementComponent getScopeElement(ActionContext ac,
			ElementComponent source) {
		ElementComponent clone = source.cloneElement(ac, null);
		clone.setDefaultValue(null);
		clone.setId((new StringBuilder(String.valueOf(source.getId()))).append(
				".t").toString());
		clone.setTdColSpan(0);
		clone.setPrefix("~");
		source.setSuffix(null);
		return clone;
	}

	private ElementComponent getOptionElement(ActionContext ac,
			ElementComponent source) {
		RadioElement option = new RadioElement();
		option.setParent(source.getParent());
		option.setId((new StringBuilder(String.valueOf(source.getId())))
				.append(".p").toString());
		option.setObject(EnumerationFactory.getEnumeration(ac, "dev",
				"queryprecision"));
		option.setDefaultValue(Integer.toString(1));
		option.setShowMode(3);
		option.setTdColSpan(0);
		option.setPrefix(" ");
		return option;
	}

	public String getOrderElementId() {
		return orderElementId;
	}

	public void setOrderElementId(String orderElementId) {
		this.orderElementId = orderElementId;
	}
}
